//todo 检查token设置接口
import { reqLogin, reqLogout, reqRegister, reqUserInfo,reqGetAuthorities, reqPostUpdateUserPassword} from "@/api/user";
import {requestGetData, requestIsSuccess} from "@/api/request";
import {removeToken} from "@/utils/token";

const state={
    code:'',
    userInfo:null,
    authorities:[]
};
const mutations={
    GETCODE(state,code){
        state.code=code;
    },
/*    USERLOGIN(state,token){
        state.token=token;
    },*/
    GETUSERINFO(state,userInfo){
        state.userInfo=userInfo;
    },
    CLEAR(state){
        removeToken();
        state.userInfo=null;
    },
    GETAUTHORITIES(state,authorities){
        state.authorities=authorities
    },
    CLEAR_USERINFO(state){
        state.userInfo = null;
    }

};
const actions={
/*    async getCode({commit},phone){
        let result = await reqGetCode(phone);
        if(result.code==200){
            commit('GETCODE',result.data)
            return 'ok'
        }else{
            return Promise.reject(new Error('getCode error'))
        }
    },*/
    async userRegister({commit},userData){
        let result = await reqRegister(userData);
        if(requestIsSuccess(result)){
            commit('GETUSERINFO',requestGetData(result))
            return true
        }
    },
    /**
     * 登录验证，保存结果
     * @param commit
     * @param loginData 登录的信息
     * @returns {Promise<boolean|*>}
     */
    async userLogin({commit},loginData){
        let result = await reqLogin(loginData);
        console.log('userLogin data:',result)
        if(requestIsSuccess(result)) {
            // commit('USERLOGIN',result.data.data.token);
            // commit('GETUSERINFO',result.data.data.token)
            // setUserInfoToken(requestGetData(result).token);
            return true;
        }
    },
    async getUserInfo({commit,getters}){
        if(getters.isLogin)
            return 'ok';
        let result = await reqUserInfo();
        if(requestIsSuccess(result)){
            commit('GETUSERINFO',requestGetData(result))
            return 'ok';
        }else{
            // removeToken()
            commit('CLEAR')
            return "not login";
        }
    },
    async logout({commit}){
        let result =  await reqLogout();
        if(requestIsSuccess(result)) {
            commit('CLEAR');
        }
        return result;
    },
    authorities({commit}){
        reqGetAuthorities().then(result=>{
            if(requestIsSuccess(result)){
                commit("GETAUTHORITIES",requestGetData(result))
            }
        })
    },
    clearUserinfo({commit}){
        commit("CLEAR_USERINFO")
    },
    async userSetPassword({commit}, password) {
        const result = await reqPostUpdateUserPassword({password: password})

        if (requestIsSuccess(result))
            return true;
    }
};
const getters={
    avatar(state){
        const imageData = state.userInfo['header_pic']||''

        return imageData
        // return imageData.indexOf(',')>0 ? imageData : `data:image/jpg;base64,${imageData}`
    },
    isLogin(state) {
        return !(state.userInfo === null || typeof(state.userInfo) === 'undefined');
    },
    username(state){
        return state.userInfo.username
    }
};
export default {
    state,mutations,actions,getters
}